package com.jiulong.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.jiulong.bean.vCard;
import com.jiulong.controller.BaseController;
import com.jiulong.service.APIService;
import com.jiulong.common.utils.FileUploadUtil;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@Controller  
@RequestMapping("api")  
public class APIController extends BaseController{  
	@Autowired  
   private APIService apiService;  
     

	
	@RequestMapping(value="/UserLocaltion",method=RequestMethod.GET)
	public void UserLocaltion(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String Log=request.getParameter("log");
		String Lat=request.getParameter("lat");
		System.out.println(Log+Lat);
		apiService.insertLocaltion(Log, Lat);
		
		
	}
	
	@RequestMapping("/Test")
	public String test(HttpServletRequest request,HttpServletResponse response) throws IOException{
//		PrintWriter printWriter = response.getWriter();
//		printWriter.print("hahahahaha");
//		printWriter.flush();
//		printWriter.close();
		return "index";
	}
	
   @RequestMapping("/UserInfo")
   public void showUser(HttpServletRequest request,HttpServletResponse response) throws DocumentException{  
	   response.setHeader("Access-Control-Allow-Origin", "*");
	   Document doc=null;
	   String json=request.getParameter("json");
	   List<String> listName=JSON.parseArray(json, String.class);
	   List UserInfo=apiService.getUserInfoByNameList(listName);
	   Iterator it=UserInfo.iterator();
	   
	   while(it.hasNext()){
		   vCard vcard=new vCard();
		   HashMap hashMap=(HashMap) it.next();
		   String id=(String)hashMap.get("username");
		   String xml=(String) hashMap.get("vcard");
		   doc=DocumentHelper.parseText(xml);
		   Element rootElt = doc.getRootElement();
		   Iterator iter = rootElt.elementIterator("N");
		   while (iter.hasNext()) {
			   Element recordEle = (Element) iter.next();
			   vcard.setName(recordEle.elementTextTrim("GIVEN"));
		   }
		   String str_json = JSON.toJSONString(vcard);
		   addData(id,vcard);
		   System.out.println(id+"&&"+vcard);
	   }
       setStatus(200, "HelloWorld");
       renderData(response);
}
	
   @RequestMapping("fileUpload")  
   public String fileUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response) {  
	   response.setHeader("Access-Control-Allow-Origin", "*");
	   response.setHeader("Access-Control-Allow-Methods","POST");
	   response.setHeader("Access-Control-Allow-Headers","Access-Control");
	   response.setHeader("Allow","POST");
	   // 判断文件是否为空  
       if (!file.isEmpty()) {  
           try {  
               // 文件保存路径  
               String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"  
                       + file.getOriginalFilename();  
               // 转存文件  
               file.transferTo(new File(filePath));  
           } catch (Exception e) {  
               e.printStackTrace();  
           }  
       }  
       // 重定向  
       return "redirect:/list.html";  
   }  
	
}